﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using e_commerce.Models;
using e_commerce.BLL.Services.Enterprise;
using e_commerce.BLL.Data;
using System.IO;
using e_commerce.Filter;
using e_commerce.BLL.Models;

namespace e_commerce.Controllers
{
    public class StoreController : Controller
    {
        private GoodsService _gService;
        public StoreController()
        {
            TempData["f-nav"] = "仓库管理";

            _gService = new GoodsService();
        }

        private void SetTempData(string fnav, string snav)
        {
            TempData["f-nav"] = fnav;
            TempData["s-nav"] = snav;
        }
        // GET: Store
        [PermissionFilter(permission: Permission.StoreManage_CreateGoods)]
        public ActionResult CreateGoods()
        {
            SetTempData("仓库管理", "新增商品");
            return View();
        }

        [PermissionFilter(permission: Permission.StoreManage)]
        public ActionResult ExistGoods(int page = 1)
        {
            var list = GetGoodsList(0, page);
            return View(list);
        }

        private PagedList.StaticPagedList<GoodsModel> GetGoodsList(int state, int page)
        {
            int total = 0;
            int pageSize = 10;
            List<GoodsModel> list = _gService.GetGoodsByPage(state,page,pageSize, ref total).Select(g => new GoodsModel()
            {
                GoodsId = g.GoodsId,
                Name = g.GoodsName,
                Stock = g.Stock,
                Price = g.Price,
                State = g.State,
                Mark = g.Details,
                StateDescribe = _gService.GetGoodsState(g.State),
            }).ToList();
            var staticList = new PagedList.StaticPagedList<GoodsModel>(list, page, pageSize, total);
            return staticList;
        }

        [HttpPost]
        [PermissionFilter(permission: Permission.StoreManage_CreateGoods)]
        public JsonResult AddGoods(NewGoods ng)
        {
            var res = new JsonResult();
            var success = "";
            var error = "";
            List<e_commerce.BLL.Data.Image> images = new List<e_commerce.BLL.Data.Image>();
            foreach(var i in ng.Images)
            {
                images.Add(new BLL.Data.Image()
                {
                    ImageId = i.Id
                });
            };
            GoodsInfo newGoods = new GoodsInfo()
            {
                GoodsName = ng.Name,
                Price = ng.Price,
                Stock = ng.Stock,
                Details = ng.Mark,
                Category = ng.Category,
                Image = images,
            };
            try
            {
                 _gService.CreateGoods(newGoods);
                success = "商品创建成功";
            }catch(Exception e)
            {
                error = e.Message;
            }
            res.Data = new { success, error };
            return res;
        }

        [HttpPost]
        public JsonResult UpLoadImg(HttpPostedFileBase file)
        {
            int intFileExtPoint = file.FileName.LastIndexOf(".");
            var fileuniName = Guid.NewGuid();
            var strFileExtName = fileuniName + "." + file.FileName.Substring(intFileExtPoint + 1).ToLower();
            var fileName = Path.Combine("E:/Images", strFileExtName);
            file.SaveAs(fileName);
            var src = "http://localhost:40000/" + strFileExtName;
            int id = _gService.SaveImage(src);
            Models.Image i = new Models.Image()
            {
                Id = id,
                Name = strFileExtName,
                AttachmentPath = src,
            };
            return Json(i);
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="goodsId"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public JsonResult ModifyGoodsInformation(GoodsModel goods)
        {
            return null;
        }
    }
}